package org.dromara.tearoom.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.tearoom.domain.TerCouponTemplate;
import org.dromara.tearoom.domain.bo.TerImgInfoBo;
import org.dromara.tearoom.domain.vo.TerImgInfoVo;
import org.dromara.tearoom.service.ITerImgInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 贴图管理
 * 前端访问路由地址为:/tearoom/imgInfo
 *
 * @author xch
 * @date 2025-05-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/imgInfo")
public class TerImgInfoController extends BaseController {

    private final ITerImgInfoService terImgInfoService;

    /**
     * 查询贴图管理列表
     */
    @SaCheckPermission("tearoom:imgInfo:list")
    @GetMapping("/list")
    public TableDataInfo<TerImgInfoVo> list(TerImgInfoBo bo, PageQuery pageQuery) {
        return terImgInfoService.queryPageList(bo, pageQuery);
    }

    @GetMapping("/wechat/list")
    public R<List<TerImgInfoVo>> list(TerImgInfoBo bo) {
        return R.ok(terImgInfoService.queryList(bo));
    }
    /**
     * 导出贴图管理列表
     */
    @SaCheckPermission("tearoom:imgInfo:export")
    @Log(title = "贴图管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TerImgInfoBo bo, HttpServletResponse response) {
        List<TerImgInfoVo> list = terImgInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "贴图管理", TerImgInfoVo.class, response);
    }

    /**
     * 获取贴图管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("tearoom:imgInfo:query")
    @GetMapping("/{id}")
    public R<TerImgInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(terImgInfoService.queryById(id));
    }

    /**
     * 新增贴图管理
     */
    @SaCheckPermission("tearoom:imgInfo:add")
    @Log(title = "贴图管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TerImgInfoBo bo) {
        return toAjax(terImgInfoService.insertByBo(bo));
    }

    /**
     * 修改贴图管理
     */
    @SaCheckPermission("tearoom:imgInfo:edit")
    @Log(title = "贴图管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TerImgInfoBo bo) {
        return toAjax(terImgInfoService.updateByBo(bo));
    }

    /**
     * 删除贴图管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("tearoom:imgInfo:remove")
    @Log(title = "贴图管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(terImgInfoService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 状态修改
     */
    @SaCheckPermission("tearoom:imgInfo:edit")
    @Log(title = "状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody TerCouponTemplate couponTemplate) {
        return toAjax(terImgInfoService.updateStatus(couponTemplate.getId(), couponTemplate.getStatus()));
    }

    /**
     * 活动列表查询
     * @param bo
     * @return
     */
    @GetMapping("/getHomeList")
    public R<List<TerImgInfoVo>> getHomeList(TerImgInfoBo bo) {
        bo.setStatus(1);
        bo.setNowDate(new Date());
        return R.ok(terImgInfoService.queryList(bo));
    }


}
